using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class ControlElement : ElementBase
    {
        private BattleUnit attractAttackTarget;
        
        public override void Execute()
        {
            base.Execute();
            AddControl();
        }

        public override void Update()
        {
            base.Update();
            if (attractAttackTarget != null)
            {
                if (attractAttackTarget.Data.isDead || attractAttackTarget != target.Data.lockTarget || attractAttackTarget != target.Data.attractAttackTarget)
                {
                    IsFree = true;
                }
            }
        }

        public override void Free()
        {
            base.Free();
            RemoveControl();
            attractAttackTarget = null;
        }

        private void RemoveControl()
        {
            var controllArg = ClassManager.Get<RemoveControllArg>();
            controllArg.element = this;
            target.Transmit(ETransmitType.RemoveControl,controllArg);
            ClassManager.Free(controllArg);
        }

        private void AddControl()
        {
            var controllArg = ClassManager.Get<AddControlArg>();

            controllArg.time = GetValue("LastTime") / 1000f;
            controllArg.controllType = config.buff_type;
            
            if (config.buff_type == 10)
            {
                int knockType = GetValue("KnockType");
                if (knockType == 0)
                {
                    controllArg.forward = (target.Data.position - bulletPosition).normalized;
                }
                else if (knockType == 1)
                {
                    controllArg.forward = (target.Data.position - source.Data.position).normalized;
                }
                
                controllArg.distance = GetValue("KnockDistance") / 1000f;
                controllArg.height = GetValue("KnockHeight") / 1000f;
            }
            else if (config.buff_type == 11)
            {
                controllArg.forward = (bulletPosition - target.Data.position).normalized;
                controllArg.distance = GetValue("DragDistance") / 1000f;
                
            }
            else if (config.buff_type == 12)
            {
                controllArg.rigidBreak = this.rigidBreak;
            }
            else if (config.buff_type == 16)
            {
                if (target.Data.GetTargetTeam(target.Data.searchTypeRpg) == source.Data.team)
                {
                    controllArg.attractAttackTarget = (BattleUnit)source;
                    attractAttackTarget = controllArg.attractAttackTarget;
                }
                else
                {
                    controllArg.attractAttackTarget = null;
                }
            }

            controllArg.element = this;
            /*if (controllArg.controllType == (int) EControlType.Hit)
            {
                target.Transmit(ETransmitType.RigidBreak,controllArg);
            }
            else*/
            {
                target.Transmit(ETransmitType.AddControl,controllArg);
            }
            
            ClassManager.Free(controllArg);
        }
    }
}